package com.vanhung.speedtest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SpeedTestLauncher extends Activity implements SpeedChecker {

	private static final String TAG = SpeedTestLauncher.class.getSimpleName();

	private TextView mTxtNetwork;

	private ImageView imgPointer;

	private Bitmap myBitmap;

	private int pointerW;

	private int pointerH;

	private AsyncDownloader downloader;

	private AsyncUploader uploader;

	boolean mCanceled = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		bindControls();
		setPointer(ZERO_POINT_DEGREE);
	}

	private void bindControls() {
		mTxtNetwork = (TextView)findViewById(R.id.networktype);

		// get a reference to the ImageView component that will display the image:
		imgPointer = (ImageView)findViewById(R.id.pointer);

		// load a resource from the res/drawable directory:
		myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pointer);

		pointerW = myBitmap.getWidth();
		pointerH = myBitmap.getHeight();
	}

	public void onStartBtnClick(View v) {
		if (Connectivity.isConnected(getApplicationContext())) {
			mCanceled = false;
			mTxtNetwork.setText("");

//			String url = "http://www.gregbugaj.com/wp-content/uploads/2009/03/dummy.txt";
//			String url = "http://download.microsoft.com/download/C/D/B/CDB0A3BB-600E-42ED-8D5E-E4630C905371/SQLSRV30.EXE";
			String url = "http://aihdownload.adobe.com/bin/live/install_reader11_uk_mssd_aih.exe";
			downloader = new AsyncDownloader(this);
			downloader.execute(url);
		}
		else {
			Toast.makeText(getApplicationContext(), R.string.toast_noconnect, Toast.LENGTH_SHORT).show();
		}
	}

	public void onCancelBtnClick(View v) {
		mCanceled = true;
		Log.d(TAG, "mCanceled = true;");
//		if (mState == TYPE_DOWNLOAD) {
//			downloader.cancel(true);
//		} else if (mState == TYPE_UPLOAD) {
//			uploader.cancel(true);
//		}
		if (downloader != null) {
			downloader.cancel(true);
		}
		if (uploader != null) {
			uploader.cancel(true);
		}
	}

	private int networkType() {
		if (Connectivity.isConnectedWifi(getApplicationContext())) {
			return 0;
		}
		else if (Connectivity.isConnectedMobile(getApplicationContext())) {
			return 1;
		}

		return -1;
	}

	protected void setPointer(double kbps) {
		final int DEGREE_OF_1M = 120;
		float degree = (float)kbps / 1024 * DEGREE_OF_1M;

		setPointer(ZERO_POINT_DEGREE + degree);
	}

	protected void setPointer(float degree) {
		// create a matrix object
		Matrix matrix = new Matrix();

		// matrix.postRotate(-30, 200, 200);
		matrix.postRotate(degree);

		// create a new bitmap from the original using the matrix to transform the result
		Bitmap rotatedBitmap = Bitmap.createBitmap(myBitmap, 0, 0, pointerW, pointerH, matrix, true);

		// display the rotated bitmap
		imgPointer.setImageBitmap(rotatedBitmap);
	}

	public void onDownloadComplete() {
		Log.d(TAG, "mCanceled " + mCanceled);
		final String url = "http://www.androidexample.com/media/UploadToServer.php";
		uploader = new AsyncUploader(this);

		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				Log.d(TAG, "mCanceled 2 " + mCanceled);
				if (!mCanceled) {
					uploader.execute(url);
				}
			}
		}, 2000);
	}

	public void onUploadComplete() {
		int i = networkType();
		if (i == 0) {
			mTxtNetwork.setText("Wifi");
		}
		else if (i == 1) {
			mTxtNetwork.setText("Mobile");
		}
		else {
			mTxtNetwork.setText("Unknown");
		}
	}

	public void onCancel() {
		Toast.makeText(getApplicationContext(), R.string.toast_cancel, Toast.LENGTH_SHORT).show();
	}
}
